package com.example.sm.mycook.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.sm.mycook.R;
import com.example.sm.mycook.url.HomeUrl;

public class HomeTeamActivity extends AppCompatActivity {

    private String path;
    private WebView wv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_team);
        path= HomeUrl.getTeamBuyHtmlPath();
        path="http://wmall.wochu.cn/h5/groupbuy/groupList.html";

        //http://wmall.wochu.cn/h5/groupbuy/groupList.html?&token=Dui15bCS7r42ewDPu9HGTvUsE-eqgwybuYeVfhot
        // WJ7bhS1MPtUwV-VLiUQXuGTUTjYQzBK8u7IkHwL58fCCr7zXXeFcYqT0zrkWlMryunIJ-chRF_WZJ6aqC_sCofTZ6YhIA
        // cqFtfU7rlbMyAwAxKEqQLLyOM8Ez8tD1Bw5EwMm9O1SkNohb70XNgmP1X19KaDISBQJNi54Gi415M7tRzYbK2L
        // tCpWPyuj941DNfn78YWCkWtgDX6OJZ89oe5MoCeRPA-LGDsF4kt9b9CiUEkxN33Vy8Q_xf0O_GAuupUbKY8EXZ
        // 6C3dUb1Dp7jjJszuDeHOAHfPLLHdJ2Mi1ebnzq-C2iODtKwOzt2lvEX6Crh-9lsdOI-kL_zw07TipZoyKRq7ED
        // 4HOb8Znb1SnP-nYNQbdQ


//        path="http://www.cnblogs.com/tinyphp/p/3858997.html";
//       Toast.makeText(this,"我进入了WebView",Toast.LENGTH_SHORT).show();
        wv = ((WebView) findViewById(R.id.wv));
        init();

    }

    private void init() {
        wv.loadUrl(path);

        WebSettings webSettings=wv.getSettings();

//        webSettings.setDefaultTextEncodingName("utf-8");
//        webSettings.setBuiltInZoomControls(true);
//        webSettings.setUseWideViewPort(true);

        webSettings.setLoadWithOverviewMode(true);
        webSettings.setJavaScriptEnabled(true);
       webSettings.setSupportMultipleWindows(true);


        wv.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                wv.loadUrl(url);
                return  false;
            }
        });


        wv.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                if (newProgress==100)
                {
//                    Toast.makeText(HomeTeamActivity.this,"网页加载完成",Toast.LENGTH_SHORT).show();

                }else {
//                    Toast.makeText(HomeTeamActivity.this,"网页加载正在完成",Toast.LENGTH_SHORT).show();
                }

            }
        });

        wv.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction()==KeyEvent.ACTION_DOWN)

                {
                    if (keyCode==KeyEvent.KEYCODE_BACK&&wv.canGoBack())
                    {
                        wv.goBack();
                        return  true;
                    }
                }
                return false;
            }
        });




    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK)
        {
            if (wv.canGoBack())
            {
                wv.goBack();
                return true;
            }else {

                System.exit(0);
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
